package com.qfedu.mtl.dao.impl;

import com.qfedu.mtl.dao.CategoryDao;
import com.qfedu.mtl.entity.Category;
import com.qfedu.mtl.query.CategoryQuery;
import com.qfedu.mtl.util.DBUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.handler.impl.BeanHandler;
import com.qfedu.mtl.util.handler.impl.BeanListHandler;
import com.qfedu.mtl.util.handler.impl.NumberHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2023/9/26 21:22
 */
public class CategoryDaoImpl implements CategoryDao {
    @Override
    public List<Category> selectData(Integer page, Integer size, CategoryQuery query) {
        List<Object> params=new ArrayList<>();
        StringBuilder sqlBuilder=new StringBuilder("select * from tb_category where 1=1");
        if (!StrUtil.isNullOrEmpty(query.getCategoryName())){
            sqlBuilder.append(" and category_name=? ");
            params.add(query.getCategoryName());
        }
        if (query.getCategoryStatus()!=null){
            sqlBuilder.append("and category_name=? ");
            params.add(query.getCategoryStatus());
        }
        sqlBuilder.append(" limit ?,?");
        params.add((page-1)*size);
        params.add(size);
        return DBUtil.executeDQL(sqlBuilder.toString(),DBUtil.getConn(),new BeanListHandler<>(Category.class,true),params.toArray());
    }

    @Override
    public Integer selectTotal(CategoryQuery query) {
        List<Object> params=new ArrayList<>();
        StringBuilder sqlBuilder=new StringBuilder("select count(*) from tb_category where 1=1");
        if (!StrUtil.isNullOrEmpty(query.getCategoryName())){
            sqlBuilder.append(" and category_name=? ");
            params.add(query.getCategoryName());
        }
        if (query.getCategoryStatus()!=null){
            sqlBuilder.append("and category_name=? ");
            params.add(query.getCategoryStatus());
        }

        return DBUtil.executeDQL(sqlBuilder.toString(),DBUtil.getConn(),new NumberHandler<>(Long.class),params.toArray()).intValue();
    }

    @Override
    public Category selectById(Integer id) {
        String sql="select * from tb_category where category_id=?";
        return DBUtil.executeDQL(sql,DBUtil.getConn(),new BeanHandler<>(Category.class,true),id);
    }

    @Override
    public int insert(Category category) {
        String sql="insert into tb_category(category_name,category_icon,category_status) values(?,?,?)";
       //category_name,category_icon,category_status
        return DBUtil.executeDML(sql,DBUtil.getConn(),category.getCategoryName(),category.getCategoryIcon(),category.getCategoryStatus());
    }

    @Override
    public int update(Category category) {
        String sql="update tb_category set category_name=?,category_icon=?,category_status=? where category_id=?";
        return DBUtil.executeDML(sql,DBUtil.getConn(),category.getCategoryName(),category.getCategoryIcon(),category.getCategoryStatus(),category.getCategoryId());
    }

    @Override
    public int delete(Integer id) {
        String sql="delete from tb_category where category_id =?";
        return DBUtil.executeDML(sql,DBUtil.getConn(),id);
    }

    @Override
    public int deleteBatch(List<Integer> ids) {
        String sql ="delete from tb_category where category_id in (";
        for (int i = 0; i < ids.size(); i++) {
            if (i!=(ids.size()-1)){
                sql+="?,";
            }else {
                sql+="?)";
            }
        }
        return DBUtil.executeDML(sql,DBUtil.getConn(),ids.toArray());
    }
}
